import { request } from '../request'

/**
 * AI总结类型
 */
export type AISummaryType = 'initial' | 'follow_up' | 'closing' | 'other'

/**
 * AI总结
 */
export interface AISummary {
  id: number
  lead_id: number
  summary_text: string
  summary_type: AISummaryType
  created_at: string
  reviewed_at: string | null
  reviewed_by: number | null
}

/**
 * 创建AI总结请求
 */
export interface AISummaryCreate {
  summary_type: AISummaryType
  prompt?: string
}

/**
 * AI标签提取结果
 */
export interface AITagExtractResult {
  processed: number
  success: number
  failed: number
  message?: string
}

/**
 * AI功能API
 * 集成后端已实现的AI智能分析功能
 */
export const aiApi = {
  /**
   * 创建AI总结
   * @param leadId 客户ID
   * @param data 创建数据
   */
  createSummary(leadId: number, data: AISummaryCreate): Promise<AISummary> {
    return request.post(`/api/v1/leads/${leadId}/ai-summary`, data)
  },

  /**
   * 获取AI总结列表
   * @param leadId 客户ID
   */
  getSummaries(leadId: number): Promise<AISummary[]> {
    return request.get(`/api/v1/leads/${leadId}/ai-summary`)
  },

  /**
   * 批量提取AI标签
   * @param batchId 导入批次ID
   */
  extractTags(batchId: string): Promise<AITagExtractResult> {
    return request.post(`/api/v1/imports/${batchId}/extract-tags`)
  },

  /**
   * 审核AI总结
   * @param summaryId 总结ID
   */
  reviewSummary(summaryId: number): Promise<AISummary> {
    return request.post(`/api/v1/leads/ai-summary/${summaryId}/review`)
  },

  /**
   * 更新AI总结
   * @param summaryId 总结ID
   * @param text 新的总结文本
   */
  updateSummary(summaryId: number, text: string): Promise<AISummary> {
    return request.patch(`/api/v1/leads/ai-summary/${summaryId}`, {
      summary_text: text,
    })
  },
}
